const globalValue = require('./global.js')
function replaceSlash(text, label) {
    if (!text || text === '') {
        return;
    }
    if (typeof (text) === 'string') {
        let newText = text.replace(/\\+/g, '\\').replace(/\//g, '\\');
        if (/^".*"$/gm.test(newText)) {
            // 移除每一行的双引号
            newText = newText.replace(/"(.*)"/gm, '$1');
        }
        if (label === 'singleRight') {
            return newText.replace(/\\/g, '/');
        }
        if (label === 'linux') {
            const matchDiskPath = newText.match(/(^[C-Zc-z]:)|(\n[C-Zc-z]:)/g);
            if (Array.isArray(matchDiskPath)) {
                const linuxDiskPath = matchDiskPath.reduce((p, cur) => p.replace(cur, cur.replace(':', '')), newText);
                return `/${linuxDiskPath.replace(/\n/g, '\n/').replace(/\\/g, '/')}`
            }
            return `a`;

        }
        if(label === 'wsl'){

            const matchDiskPath = newText.match(/(^[C-Zc-z]:)|(\n[C-Zc-z]:)/g);
            if (Array.isArray(matchDiskPath)) {
                const wslDiskPath = matchDiskPath.reduce((p, cur) => {
                    // Convert disk letter to lowercase before replacing
                    const lowerCaseDisk = cur.replace(':', '').toLowerCase();
                    return p.replace(cur, lowerCaseDisk);
                }, newText);
                return `/mnt/${wslDiskPath.replace(/\n/g, '\n/mnt/').replace(/\\/g, '/')}`;
            }
            return `a`;
        }
        return newText.replace(/\\/g, '\\\\')
    }
}
const labelMapOperation = {
    'windowsPath': 'doubleLeft',
    'windowsPathRight': 'singleRight',
    'windowsPathLinux': 'linux',
    'windowsPathWsl': 'wsl',
}
function debounce(fn, delay) {
    let timer = null
    return function (...args) {
        if (timer) {
            clearTimeout(timer)
            timer = setTimeout(fn.bind(null, ...args), delay)
        } else {
            timer = setTimeout(fn.bind(null, ...args), delay)
        }
    }
}
const setText = debounce((newText) => {
    const lastNewText = String(globalValue.lastTransformText);

    if (lastNewText === newText) {
        return;
    }
    const delTextStatus = lastNewText.length > String(newText).length;
    globalValue.lastTransformText = newText;
    // 删除就再等等
    if (delTextStatus) {
        waitDelEndSetText(newText)
        return
    }
    window.utools.setSubInputValue(newText);
}, 370);
const waitDelEndSetText = debounce((newText) => {
    setText(newText)
}, 490);
const windowsPathConfig = {
    mode: 'none',
    args: {
        enter: (action) => {

            const { payload, type, label, code, text, noCheck } = action;
            if (type === "files") {
                const res = payload.map(item => replaceSlash(item.path, labelMapOperation[code]));
                window.utools.copyText(res.join('\n'))
            }
            if (type === 'regex') {
                window.utools.copyText(replaceSlash(payload, labelMapOperation[code]))
            }
            if (type === 'text') {
                const newText = replaceSlash(text, labelMapOperation[code])
                if (noCheck) {
                    window.utools.setSubInputValue(newText);
                }
                if (text && text != newText) {
                    setText(newText);
                }
            }
        },
    }
}
module.exports = windowsPathConfig;
